<?php
/**
 *
 * Created by PhpStorm.
 * User: Egor <http://gitee.com/seosite>
 * Date: 2018年04月17日
 * Time: 15:38:25
 */
namespace Wap\Controller;

use Faker\Factory;
use Faker\Provider\DateTime;
use Think\Controller;
header('content-type:text/html;charset=utf-8');
class UserController extends BaseController
{

    /**
     * @desc 注册
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
	public function register($param){

	    $check_map['phone'] = $param['phone'];

	    $model = M('User');

		$check_res = $model->where($check_map)->find();

		if(!empty($check_res)) return showErrorMsg(L('_REGISTER_PHONE_EXIST_'),100002);

		if(empty($param['password']) || empty($param['repassword']) || empty($param['smscode'])) return showErrorMsg('请完善注册信息后提交',100002);
		
		if($param['password'] !== $param['repassword']) return showErrorMsg('密码填写不一致',100002);

		$sms_map['phone'] = $param['phone'];

		$smscode = $param['smscode'];

		$SmsModel = M('Sms');

		$Smsdata = $SmsModel->where($sms_map)->order('id DESC')->find();


		$Smsnowtime = NOW_TIME;

		$Smstemptime = $Smsdata['create_time']+60;

		if($Smsnowtime > $Smstemptime) return showErrorMsg($msg = L('_REGISTER_SMSCODE_INVALID_'),$code = 100002);

		if($Smsdata['code'] != $smscode) showErrorMsg($msg = L('_SMSCODE_ERROR_'),$code = 100002);


        $faker = Factory::create();

        $nickname = $faker->userName;
        $insert_data = array(
            'u_no'      =>  setUno(),
            'source'    =>  1,
            'role'      =>  1,//用户角色：1 普通用户，2 A博主(权限大)，3 B博主(权限小)
            'user_type' =>  1,//博主用户身份：1 个人，2机构
            'bo_status' =>  1,//博主审核状态：0 初始化，1 正常，2 审核未通过，3审核中
            'nickname'  =>  $nickname,
            'realname'  =>  '',
            'company'   =>  '',
            'career'    =>  '',
            'headsmall' =>  $faker->imageUrl(150,150),
            'email_address' =>  '',
            'email'     =>  '',
            'phone'     =>  $param['phone'],
            'website'   =>  $faker->url,
            'birthdaytime'  =>  DateTime::date().' '.DateTime::time(),
            'password'  =>  md5($param['password']),
            'sex'       =>  1,
            'signature' =>  $faker->text(50),
            'balance'   =>  '',
            'integral'  =>  '',
            'level'     =>  '',
            'freeze'    =>  '',
            't_id'      =>  '',
            't_add'     =>  '',
            'qrcode'    =>  '',
            'status'    =>  1,
            'realname_status'   =>  0,
            'idnumber'  =>  '',
            'phone_area'    =>  86,
            'logins'    =>  0,
            'last_ip'   =>  '',
            'last_time' =>  '',
            'reg_ip'    =>  client_ip(),
            'reg_time'  =>  time(),
            'u_intro'   =>  '',
            'c_intro'   =>  '',
            'company_add'   =>  '',
            'income'    =>  '',
            'outcome'   =>  '',
            'rank'      =>  '',
            'j_name'    =>  '',
            'j_contacts'    =>  '',
            'show_type' =>  1,
            'qq_openid' =>  '',
            'wechat_openid' =>  '',
            'weibo_openid'  =>  '',
            'first_name'    =>  get_first_char($nickname),
        );

        $operation = $model -> add($insert_data);

		if($operation)  {

		    M('sms')->where(['phone'=>$param['phone']])->delete();

		    $userInfo = $model->where(['id'=>$operation])->find();

            $imObj = new ImController();
            $im_uuid = $imObj->registerByUno($insert_data['u_no'],$insert_data['nickname'],$insert_data['headsmall']);

            M('user')->where(['id'=>$operation])->setField('im_uuid',$im_uuid);

            $user_auth_add = array(
                'title' =>  'qa',
                'auth'  =>  0,
                'fee'   =>  '',
                'pwd'   =>  '',
                'uid'   =>  $operation
            );

            M('user_auth')->add($user_auth_add);

            $user_nav_list[] = ['modul_name'=>'ucenter','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
            $user_nav_list[] = ['modul_name'=>'message','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
            $user_nav_list[] = ['modul_name'=>'site','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
            $user_nav_list[] = ['modul_name'=>'finance','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
            $user_nav_list[] = ['modul_name'=>'qa','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
            $user_nav_list[] = ['modul_name'=>'news','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
//            $user_nav_list[] = ['modul_name'=>'video','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
//            $user_nav_list[] = ['modul_name'=>'audio','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
//            $user_nav_list[] = ['modul_name'=>'report','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
//            $user_nav_list[] = ['modul_name'=>'picture','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
            $user_nav_list[] = ['modul_name'=>'data','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
            $user_nav_list[] = ['modul_name'=>'meeting','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
//            $user_nav_list[] = ['modul_name'=>'question','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
            $user_nav_list[] = ['modul_name'=>'connect','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
            $user_nav_list[] = ['modul_name'=>'collect','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
            M('user_nav')->addAll($user_nav_list);


		    $token = _set_token($userInfo,0,0);

            $userInfo = decode_token($token);

            $userInfo['token']    = $token;

		    return showErrorMsg( L('_OPERATION_SUCCESS_'),0, 2,$userInfo);

        }else{

            return showErrorMsg( L('_INSERT_ERROR_'), 100002);

        }
	}

    /**
     * @desc 登录
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function login($param)
    {
        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('password,username,remember');

        unset($check);

        $password = md5($param['password']);
        $username = $param['username'];

        //过期时间，记住设置为0不过期，不记住 设置token失效时间为3天
        $expire_time = empty($param['remember'])?259200:0;

        $model = M('user');
        $map['u_no']            = $username;
        $map['phone']           = $username;
        $map['nickname']        = $username;
        $map['_logic']          ='or';
        $condition['_complex']  = $map;
        $condition['password']  = $password;

        $userinfo = $model->where($condition)->find();


        $admin_map['username']  =   $username;
        $admin_map['phone']     =   $username;
        $admin_map['_logic']          ='or';
        $admin_condition['_complex']  = $admin_map;
        $admin_condition['password']  = $password;
        $admin_condition['is_wap']  = 1;

        $adminUserinfo = M("admin")->where($admin_condition)->find();

        if($userinfo){
            if($userinfo['status']!=0){
                //nav List
                $nav_list = M('user_nav')->where(['uid'=>$this->userInfo->id])->select();

                $userinfo['logins']     = $userinfo['logins'] + 1;
                $userinfo['last_ip']    = client_ip();
                $userinfo['last_time']  = time();
                $reslogin   = $model->save($userinfo);

                if($reslogin){

                    $userinfo['is_admin']   = 0;
                    $userinfo['nav']    =   $nav_list;

                    $token      = _set_token($userinfo,0,$expire_time);
                    $userinfo   = decode_token($token);
                    $userinfo['token']    = $token;


                    $ImObj = new ImController();
                    $userinfo['ImToken'] = $ImObj->getImToken($userinfo['id']);
                    if($token)  return showErrorMsg($msg = L('_OPERATION_SUCCESS_'),$code = 0,$return_type = 2,$return_data = $userinfo);
                    if(!$token) return showErrorMsg($msg = L('_TOKEN_BUILD_ERROR_'),$code = 100004);
                }else{
                    return showErrorMsg($msg = L('_TOKEN_BUILD_ERROR_'),$code = 100002);
                }
            }else{
                return showErrorMsg($msg = L('_ACCOUNT_DISABLED_'),$code = 100002);
            }
        }elseif($adminUserinfo){
            if($adminUserinfo['status']==1){
                $admin_save_data['id']=$adminUserinfo['id'];
                $admin_save_data['last_ip']    = client_ip();
                $admin_save_data['last_time']  = time();
                $reslogin   = M('admin')->save($admin_save_data);
                if($reslogin){
                    $adminUserinfo['is_admin']   = 1;
                    $adminUserinfo['nav']    =   [];

                    $admin_token      = _set_token($adminUserinfo,1,$expire_time);
                    $admin_info   = decode_token($admin_token);
                    $admin_info['token']    = $admin_token;
                    $admin_info['ImToken'] = '';
                    if($admin_token)  return showErrorMsg($msg = L('_OPERATION_SUCCESS_'),$code = 0,$return_type = 2,$return_data = $admin_info);
                    if(!$admin_token) return showErrorMsg($msg = L('_TOKEN_BUILD_ERROR_'),$code = 100004);
                }else{
                    return showErrorMsg($msg = L('_TOKEN_BUILD_ERROR_'),$code = 100002);
                }
            }else{
                return showErrorMsg($msg = L('_ACCOUNT_DISABLED_'),$code = 100002);
            }
        }else{
            return showErrorMsg($msg = L('_LOGIN_ERROR_'),$code = 100002);
        }
    }

    /**
     * @desc 用户详情
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function userDetail()
    {
        $check = CheckFactoryController::isLogin();
        $check->before(null);

        unset($check);

        $field = 'id,u_no,headsmall,realname,nickname,website,phone,company,email,career,email_address,email,balance,income,outcome,rank,u_intro,c_intro,role,user_type,bo_status,j_name,j_contacts,show_type,website';
        $userInfo = M('User')->field($field)->find($this->userInfo->id);

        list($userInfo['rank'],$userInfo['rank_text']) = userRank($this->userInfo->id);
        //connect fields
        $tag_map['type'] = 12;
        $tag_map['uid'] = 0;
//        $tag_map['uid'] = array('in','0,'.$this->userInfo->id);
        $tagList = M('tag')->field('id,title,uid')->where($tag_map)->select();
        if (!empty($tagList)) {
            foreach ($tagList as &$v){
                $is_tag = M('work_tag')->where(['type'=>12,'work_id'=>$this->userInfo->id,'tag_id'=>$v['id']])->find();
                $v['is_select'] = $is_tag ?1:0;
            }
            unset($v);
        }
        $nav_list = M('user_nav')->where(['uid'=>$this->userInfo->id])->select();

        $userInfo['nav'] = $nav_list;
        $userInfo['tag'] = $tagList;

        $message_map['uid'] = $this->userInfo->id;
        $messageCount = M('message')->where($message_map)->count();
        $userInfo['message_count'] = empty($messageCount)?0:(int)$messageCount;

        $connect_map['uid'] = $this->userInfo->id;
        $connectCount = M('user_connect')->where($connect_map)->count();
        $userInfo['user_connect'] = empty($connectCount)?0:(int)$connectCount;

        $follow_map['puid'] = $this->userInfo->id;
        $followCount = M('user_connect')->where($follow_map)->count();
        $userInfo['user_follow'] = empty($followCount)?0:(int)$followCount;

        $bank_map['uid'] = $this->userInfo->id;
        $is_band_bank = M('bank')->where($bank_map)->select();
        $userInfo['bankList'] = $is_band_bank;
        $userInfo['is_band_bank'] = empty($is_band_bank)?2:1;

        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$userInfo);
    }

    /**
     * @desc 修改密码
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array|bool
     */
    public function updatePwd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before(null);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);


        $check = CheckFactoryController::isChildParamSet();
        $check->before('type');

        if ($param['type']==1){
            $check = CheckFactoryController::isChildParamSet();
            $check->before('phone,code,pwd,repwd');
            if (empty($param['phone']) || empty($param['code']) || empty($param['pwd']) || empty($param['repwd'])) return showErrorMsg('值不能为空',100002);

            if ($param['pwd']!=$param['repwd']) return showErrorMsg('密码不一致',100002);

        }else{
            $check = CheckFactoryController::isChildParamSet();
            $check->before('oldpwd,pwd,repwd');

            if (empty($param['oldpwd']) || empty($param['pwd']) || empty($param['repwd'])) return showErrorMsg('值不能为空',100002);
            if ($param['pwd']!=$param['repwd']) return showErrorMsg('密码不一致',100002);

            if ($param['oldpwd'] == $param['pwd']) return showErrorMsg('新旧密码不能相同',100002);
        }

        unset($check);

        $userObj = M('User');
        $smsObj = M('sms');

        switch ($param['type']){
            case 1:
                $sms_map['phone'] = $param['phone'];
                $sms_map['type'] = 2;

                $smsInfo = $smsObj->where($sms_map)->find();

                if (!empty($smsInfo) && ($smsInfo['create_time']+60 ) > NOW_TIME){

                    $operation = $userObj->where(['id'=>$this->userInfo->id])->setField('password',md5($param['pwd']));

                    if ($operation) return showErrorMsg(L('_OPERATION_SUCCESS_'));
                    return showErrorMsg(L("_OPERATION_FAIL_"),100002);

                }else{
                    return showErrorMsg('验证码已失效',100002);
                }

                break;
            case 2:

                $userInfo = $userObj->where(['id'=>$this->userInfo->id])->find();

                if (md5($param['oldpwd']) != $userInfo['password']) return showErrorMsg('旧密码不匹配',100002);

                $operation = $userObj->where(['id'=>$this->userInfo->id])->setField('password',$param['pwd']);
                if ($operation) return showErrorMsg(L('_OPERATION_SUCCESS_'));
                return showErrorMsg(L("_OPERATION_FAIL_"),100002);
                break;
            default:
                return false;
        }
    }

    /**
     * @desc 修改绑定手机号
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function updateBandPhone($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before(null);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('phone,code');

        $model = M('sms');

        $is_exist = $model->where(['phone'=>$param['phone'],'code'=>$param['code'],'type'=>3])->find();

        if (empty($is_exist)) return showErrorMsg('请先去获取验证码',100002);

        if ( (NOW_TIME - $is_exist['create_time'])>60 ) return showErrorMsg('短信验证码已失效，请重新获取验证码',100002);

        $operation = M('User')->where(['id'=>$this->userInfo->id])->setField('phone',$param['phone']);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),100002);
    }

    /**
     * @desc 修改绑定邮箱
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function updateBandEmail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before(null);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('email,code');

        $is_exist = M('send_email')->where(['email'=>$param['email'],'code'=>$param['code'],'type'=>2])->find();

        if (!$is_exist) return showErrorMsg('没有查询到邮箱',100002);

        if ( ($is_exist['create_time'] + 60)> NOW_TIME){
            $operation = M('User')->where(['id'=>$this->userInfo->id])->setField('email',$param['email']);

            if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
            return showErrorMsg(L("_OPERATION_FAIL_"),100002);
        }else{
            return showErrorMsg('验证码已过期，请在60秒内操作。',100002);
        }
    }

    /**
     * @desc 更新个人资料
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function updateUserInfo($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before(null);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        unset($check);
        $userModel = M('User');

        $save_data = array();

        if ($param['nickname']) $save_data['nickname'] = $param['nickname'];
        if ($param['company']) $save_data['company'] = $param['company'];
        if ($param['career']) $save_data['career'] = $param['career'];
        if ($param['email_address']) $save_data['email_address'] = $param['email_address'];
        if ($param['show_type']) $save_data['show_type'] = $param['show_type'];
        if ($param['u_intro']) $save_data['u_intro'] = $param['u_intro'];
        if ($param['c_intro']) $save_data['c_intro'] = $param['c_intro'];
        if ($param['headsmall']) $save_data['headsmall'] = $param['headsmall'];
        if ($param['j_contacts']) $save_data['j_contacts'] = $param['j_contacts'];
        if ($param['website']) $save_data['website'] = $param['website'];
        if ($param['realname']) $save_data['realname'] = $param['realname'];
        if ($param['j_name']) $save_data['j_name'] = $param['j_name'];

        if ($param['nickname']) {
            $first_name = getInitials(mb_substr($param['nickname'],0,1));
            $save_data['first_name'] = $first_name;
        }

        if ($param['tag_id']){
            M('work_tag')->where(['type'=>12,'work_id'=>$this->userInfo->id])->delete();

            $tagArr = explode(',',$param['tag_id']);

            foreach ($tagArr as &$v){
                $insert_tag = array(
                    'tag_id'  =>  $v,
                    'type'  =>  12,
                    'work_id'   =>  $this->userInfo->id,
                    'create_time'   =>  time(),
                );
                M('work_tag')->add($insert_tag);
            }
        }

        $operation = $userModel->where(['id'=>$this->userInfo->id])->save($save_data);

        if ($operation) return showErrorMsg(L('_OPERATION_SUCCESS_'));

        return showErrorMsg(L("_OPERATION_FAIL_",100002));
    }


    public function dateAnalysis($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before(null);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('email,code');
    }

	/**
	 * @desc 找回密码 
	 * @param $phone     用户账户  
	 * @param $password  用户密码
	 * @return string
	 */
	public function forgotpwd($data){
		$smscode     =  $data['smscode'];
		$Sms = M("Sms"); // 实例化User对象
		$Smsdata = $Sms->where('phone ='.$data['phone'])->order('id DESC')->find();
		$Smsnowtime = time();
		$Smstemptime = $Smsdata['create_time'] + 60;
		if($Smsnowtime<=$Smstemptime){
			if($Smsdata['code']==$smscode){
				$map['phone'] = $data['phone'];
				$userInfo = M("User")->where($map)->find();
                $data['id'] = $userInfo['id'];

				$data['password'] = md5($data['password']);
				if ($data['password']==$userInfo['password']) return showErrorMsg('新旧密码不能相同',100002);
				$Re = M("User") ->save($data);
				if($Re)  return showErrorMsg(L('_OPERATION_SUCCESS_'),0);
				if(!$Re) return showErrorMsg($msg = L('_OPERATION_FAIL_'),$code = 100002);
			}
			if($Smsdata['code']!=$smscode)	return showErrorMsg($msg = L('_SMSCODE_ERROR_'),$code = 100002);
		}
		if($Smsnowtime > $Smstemptime)  return showErrorMsg($msg = L('_REGISTER_SMSCODE_INVALID_'),$code = 100002);

	}

    /**
     * @desc 修改密码，需要登录
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @param $data
     * @return array
     */
    public function changePwd($data){
        if (empty($this->userInfo)) return showErrorMsg(L('_TOKEN_USELESS_'),100004);

        $phone =M('user')->where(array('id'=>$this->userInfo->id))->getField('phone');
        $smscode     =  $data['smscode'];
        $Sms = M("Sms"); // 实例化User对象
        $Smsdata = $Sms->where('phone ='.$phone)->order('id DESC')->find();
        $Smsnowtime = time();
        $Smstemptime = $Smsdata['sendtime'] + 60;
        if($Smsnowtime<=$Smstemptime){
            if($Smsdata['code']==$smscode){
                $map['phone'] = $phone;
                $userInfo = M("User")->where($map)->find();
                $data['id'] = $userInfo['id'];

                $data['password'] = md5($data['password']);
                if ($data['password'] == $userInfo['password']) return showErrorMsg('新旧密码不能相同',100002);
                $Re = M("User") ->save($data);
                if($Re)  return showErrorMsg(L('_OPERATION_SUCCESS_'),0);
                if(!$Re) return showErrorMsg($msg = L('_OPERATION_FAIL_'),$code = 100002);
            }
            if($Smsdata['code']!=$smscode)	return showErrorMsg($msg = L('_SMSCODE_ERROR_'),$code = 100002);
        }
        if($Smsnowtime > $Smstemptime)  return showErrorMsg($msg = L('_REGISTER_SMSCODE_INVALID_'),$code = 100002);

    }

	/**
	 * @desc 退出登陆 
	 * @return string
	 */
	public function logout(){
        if (empty($this->userInfo)) return showErrorMsg(L('_TOKEN_USELESS_'),100004);
		$map['uid'] = $this->userInfo->id;
		$Re = M('Token')->where($map)->delete();
		if($Re)  return showErrorMsg($msg = L('_OPERATION_SUCCESS_'),$code = 0,$return_type = 2,$return_data = $map);
		if(!$Re) return showErrorMsg($msg = L('_OPERATION_FAIL_'),$code = 100002);
	}

	/**
	 * @desc 更新用户信息
	 * @param $sex           用户性别 
	 * @param $nickname      用户昵称
	 * @param $headimgurl    用户头像
	 * @param $birthdaytime  用户生日   2017-09-01
	 * @return string
	 */
	public function userupdate($data){

        if (empty($this->userInfo)) return showErrorMsg(L('_TOKEN_USELESS_'),100004);

		$User = M("User");
		if($data['headsmall']) $insertdata['headsmall'] = $data['headsmall'];
		if($data['sex']) $insertdata['sex'] = $data['sex'];
		if($data['nickname'])$insertdata['nickname'] = $data['nickname'];
		if($data['career_id'])$insertdata['career_id'] = $data['career_id'];
		if($data['birthdaytime']) $insertdata['birthdaytime'] = strtotime($data['birthdaytime']);
		if($data['signature']) $insertdata['signature'] = $data['signature'];
		$map['id'] = $this->userInfo->id;
		$Re = $User->where($map)->save($insertdata);
		$UserInfo = $User->find($this->userInfo->id);
		$token = _set_token($UserInfo,0,0);
        $UserInfo['phone'] = substr_replace($UserInfo['phone'],'****',3,4);
        $UserInfo['password'] = 'CJCJCJCJ!';
        $UserInfo['token'] = $token;
        $RecordObj = new RecordController();
        list($UserInfo['rank'],$UserInfo['rank_text']) = $RecordObj->userRank($UserInfo['id']);
        if (empty($UserInfo['career_id'])){
            $UserInfo['career_text'] = '';
        }else{
            $UserInfo['career_text'] = M('career')->where(array('id'=>$UserInfo['career_id']))->getField('title');
        }
		if($Re)  return showErrorMsg($msg = L('_OPERATION_SUCCESS_'),$code = 0,$return_type = 2,$return_data = $UserInfo);
		if(!$Re) return showErrorMsg($msg = L('_OPERATION_FAIL_'),$code = 100002);
	}

    /**
     * @desc 职业列表
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @return array
     */
	public function careerList()
    {
        list($Retlist,$page) = _listPage(M('career'),null,1,null,null,100,null);
        return showErrorMsg('ok',0,1,$Retlist,$page);
    }

    /**
     * @desc 查看实名认证
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     */
    public function catRealAuth()
    {
        if (empty($this->userInfo)) return showErrorMsg(L('_TOKEN_USELESS_'),100004);

        $ret = M('user')->field('id,realname_status,realname,idnumber')->where(array('id'=>$this->userInfo->id))->find();
        return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,$ret);
    }

    /**
     * @desc 修改实名认证信息
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @param $param
     * @return array
     */
    public function updateRealAuth($param)
    {
        if (empty($this->userInfo)) return showErrorMsg(L('_TOKEN_USELESS_'),100004);
        $realname_status = M('user')->where(array('id'=>$this->userInfo->id))->getField('realname_status');

        if ($realname_status==1) return showErrorMsg(L('_STATUS_IN_PASS_'),100002);
        if ($realname_status==3) return showErrorMsg(L('_STATUS_IN_SH_'),100002);

        $insetData = array(
            'realname'  =>  $param['realname'],
            'idnumber'  =>  $param['idNumber']
        );

        M('user')->where(array('id'=>$this->userInfo->id))->save($insetData);
        M('user')->where(array('id'=>$this->userInfo->id))->setField('realname_status',3);
        return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,new \stdClass());

    }

    /**
     * @desc 修改手机号
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @param $param
     * @return array
     */
    public function updatePhone($param)
    {
        if (empty($this->userInfo)) return showErrorMsg(L('_TOKEN_USELESS_'),100004);

        $phone = $param['phone'];

        $is_exist = M('user')->where(array('phone'=>$phone))->find();

        if ($is_exist) return showErrorMsg(L('_REGISTER_PHONE_EXIST_'),100002);
        $User = M("User"); // 实例化User对象
        $smscode     =  $param['smscode'];
        $Sms = M("Sms"); // 实例化User对象
        $Smsdata = $Sms->where(array('phone'=>$param['phone'],'type'=>4))->order('id DESC')->find();
        $Smsnowtime = time();
        $Smstemptime = $Smsdata['sendtime'] + 60;
        if($Smsnowtime<=$Smstemptime){
            if($Smsdata['code']==$smscode){
                $Re = $User->where(array('id'=>$this->userInfo->id))->setField('phone',$phone);
                $Uinfo = $User->where(array('id'=>$this->userInfo->id))->find();
                $token = _set_token($Uinfo,0,0);
                $Uinfo = decode_token($token);
                $Uinfo['token'] = $token;

                if($Re)  return showErrorMsg($msg = L('_OPERATION_SUCCESS_'),$code = 0,$return_type = 2,$Uinfo);
                if(!$Re) return showErrorMsg($msg = L('_PHONE_REPLAY_'),$code = 100002);
            }
            if($Smsdata['code']!=$smscode)	return showErrorMsg($msg = L('_SMSCODE_ERROR_'),$code = 100002);
        }
        if($Smsnowtime > $Smstemptime)  return showErrorMsg($msg = L('_REGISTER_SMSCODE_INVALID_'),$code = 100002);

    }

    /**
     * @desc 注销账号
     */
    public function abortAccount()
    {
        if (empty($this->userInfo)) return showErrorMsg(L('_TOKEN_USELESS_'),100004);

        M('user')->delete($this->userInfo->id);
        M('message')->where(['user_id'=>$this->userInfo->id])->delete();

        return showErrorMsg(L('_OPERATION_SUCCESS_'));
    }

    /**
     * @desc 三方登录
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function otherLogin($param)
    {
        $check = CheckFactoryController::isChildParamSet();
        $check->before('type');

        unset($check);

        $type = $param['type'];
        $model = M('User');

        switch ($type){
            case 'wechat':
                $openid = $param['openid'];

                $is_exist = $model->where(['wechat_openid'=>$openid])->find();

                if ($is_exist){

                    if (empty($is_exist['phone'])){
                        $token = _set_token($is_exist,0);
                        $info = decode_token($token);
                        $info['token']    = $token;
                        if($token) return showErrorMsg($msg = $token,$code = 100009,$return_type = 2,$return_data = $info);
                        return showErrorMsg($msg = L('_TOKEN_BUILD_ERROR_'),$code = 100004);
                    }
                    if ($is_exist['role']==1) return showErrorMsg('只有博主才可以登录哦QAQ..',100002);
                    if ($is_exist['bo_status']==0||$is_exist['bo_status']==3) return showErrorMsg('QaQ...博主资料审核中',100002);
                    if ($is_exist['bo_status']==2) return showErrorMsg('QaQ...博主资料未通过审核',100002);
                    if ($is_exist['status']==2) return showErrorMsg('用户已被禁用',100002);

                    $userinfo['id'] = $is_exist['id'];
                    $userinfo['logins'] = $is_exist['logins'] + 1;
                    $userinfo['last_ip'] = client_ip();
                    $userinfo['last_time'] = time();
                    $reslogin = $model->save($userinfo);

                    if($reslogin){
                        $ImObj = new ImController();
                        $is_exist['ImToken'] = $ImObj->getImToken($userinfo['id']);
                        $token = _set_token($is_exist,0);
                        $info = decode_token($token);
                        $info['token']    = $token;
                        if($token) return showErrorMsg($msg = L('_OPERATION_SUCCESS_'),$code = 0,$return_type = 2,$return_data = $info);
                        return showErrorMsg($msg = L('_TOKEN_BUILD_ERROR_'),$code = 100004);
                    }else{
                        return showErrorMsg($msg = L('_TOKEN_BUILD_ERROR_'),$code = 100002);
                    }
                }else{

                    $insert_data = array(
                        'u_no'      =>  setUno(),
                        'role'      =>  1,
                        'nickname'  =>  $param['nickname'],
                        'headsmall'  =>  $param['headimgurl'],
                        'wechat_openid'  =>  $param['openid'],
                        'reg_time'  =>  NOW_TIME,
                        'reg_ip'  =>  client_ip(),
                        'first_name'    =>  get_first_char($param['nickname'])
                    );
                    $operation = $model->add($insert_data);

                    if ($operation) {
                        $imObj = new ImController();
                        $im_uuid = $imObj->register($operation);

                        $model->where(['id'=>$operation])->setField('im_uuid',$im_uuid);

                        $user_auth_add = array(
                            'title' =>  'qa',
                            'auth'  =>  0,
                            'fee'   =>  '',
                            'pwd'   =>  '',
                            'uid'   =>  $operation
                        );

                        M('user_auth')->add($user_auth_add);

                        $user_nav_list[] = ['modul_name'=>'ucenter','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['modul_name'=>'message','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['modul_name'=>'site','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['modul_name'=>'finance','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['modul_name'=>'qa','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['modul_name'=>'data','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['modul_name'=>'meeting','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['modul_name'=>'connect','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['modul_name'=>'collect','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['modul_name'=>'news','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        M('user_nav')->addAll($user_nav_list);

                        $userinfo = $model->find($operation);
                        $ImObj = new ImController();
                        $userinfo['ImToken'] = $ImObj->getImToken($userinfo['id']);
                        $token = _set_token($userinfo,0);
                        $info = decode_token($token);
                        $info['token']    = $token;
                        if($token) return showErrorMsg($msg = $token,$code = 100009,$return_type = 2,$return_data = $info);
                        return showErrorMsg($msg = L('_TOKEN_BUILD_ERROR_'),$code = 100004);
                    }else{
                        return showErrorMsg('绑定失败',100002);
                    }
                }

                break;
            case 'qq':
                $openid = $param['openid'];

                $is_exist = $model->where(['qq_openid'=>$openid])->find();

                if ($is_exist){

                    if (empty($is_exist['phone'])) {
                        $token = _set_token($is_exist,0);
                        $is_exist = decode_token($token);
                        $is_exist['token']    = $token;
                        if($token) return showErrorMsg($msg = $token,$code = 100009,$return_type = 2,$return_data = $is_exist);
                        return showErrorMsg($msg = L('_TOKEN_BUILD_ERROR_'),$code = 100004);
                    }
                    if ($is_exist['role']==1) return showErrorMsg('只有博主才可以登录哦QAQ..',100002);
                    if ($is_exist['bo_status']==0||$is_exist['bo_status']==3) return showErrorMsg('QaQ...博主资料审核中',100002);
                    if ($is_exist['bo_status']==2) return showErrorMsg('QaQ...博主资料未通过审核',100002);
                    if ($is_exist['status']==2) return showErrorMsg('用户已被禁用',100002);

                    $userinfo['id']=$is_exist['id'];
                    $userinfo['logins'] = $is_exist['logins'] + 1;
                    $userinfo['last_ip'] = client_ip();
                    $userinfo['last_time'] = time();
                    $reslogin = $model->save($userinfo);

                    if($reslogin){
                        $ImObj = new ImController();
                        $is_exist['ImToken'] = $ImObj->getImToken($userinfo['id']);
                        $token = _set_token($is_exist,0);
                        $Info = decode_token($token);
                        $Info['token']    = $token;
                        if($token) return showErrorMsg($msg = L('_OPERATION_SUCCESS_'),$code = 0,$return_type = 2,$return_data = $Info);
                        return showErrorMsg($msg = L('_TOKEN_BUILD_ERROR_'),$code = 100004);
                    }else{
                        return showErrorMsg($msg = L('_TOKEN_BUILD_ERROR_'),$code = 100002);
                    }
                }else{

                    $insert_data = array(
                        'u_no'      =>  setUno(),
                        'role'      =>  1,
                        'nickname'  =>  $param['nickname'],
                        'headsmall'  =>  $param['figureurl_qq_2'],
                        'qq_openid'  =>  $param['openid'],
                        'reg_time'  =>  NOW_TIME,
                        'reg_ip'  =>  client_ip(),
                        'first_name'    =>  get_first_char($param['nickname'])
                    );
                    $operation = $model->add($insert_data);

                    if ($operation) {
                        $imObj = new ImController();
                        $im_uuid = $imObj->register($operation);

                        $model->where(['id'=>$operation])->setField('im_uuid',$im_uuid);

                        $user_auth_add = array(
                            'title' =>  'qa',
                            'auth'  =>  0,
                            'fee'   =>  '',
                            'pwd'   =>  '',
                            'uid'   =>  $operation
                        );

                        M('user_auth')->add($user_auth_add);

                        $user_nav_list[] = ['modul_name'=>'ucenter','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['modul_name'=>'message','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['modul_name'=>'site','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['modul_name'=>'finance','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['modul_name'=>'qa','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['modul_name'=>'data','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['modul_name'=>'meeting','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['modul_name'=>'connect','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['modul_name'=>'collect','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        $user_nav_list[] = ['news'=>'collect','uid'=>$operation,'status'=>1,'deadline'=>0,'create_time'=>time()];
                        M('user_nav')->addAll($user_nav_list);

                        $userinfo = $model->find($operation);
                        $ImObj = new ImController();
                        $userinfo['ImToken'] = $ImObj->getImToken($userinfo['id']);
                        $token = _set_token($userinfo,0);
                        $info = decode_token($token);
                        $info['token']    = $token;
                        if($token) return showErrorMsg($msg = $token,$code = 100009,$return_type = 2,$return_data = $info);
                        return showErrorMsg($msg = L('_TOKEN_BUILD_ERROR_'),$code = 100004);
                    }else{
                        return showErrorMsg('绑定失败',100002);
                    }
                }
                break;
            case 'weibo':
                break;
            default:
                return showErrorMsg('第三方登录方式错误');
        }
    }


}